// pages/home/shopcar/index.js
var config = require('../../../../config');
var strings = require('../../../../strings.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsList: [{ isChoice: true, buyNum: 1, price: 65 }, { isChoice: true, buyNum: 1, price: 45 }, { isChoice: true, buyNum: 1, price: 85 }, { isChoice: true, buyNum: 1, price: 165},],
    allChoice: true,  //购物车商品是否全选
    allMoney: 0,  //合计金额
    goodsCount: 0,  //勾选的商品
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.countMoney()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getShopCarData()
  },
  /**
   * 获取购物车数据
   */
  getShopCarData: function() {
    var that = this
    wx.request({
      url: config.CouponsCarGoods,
      method: 'GET',
      success: function(res) {
        console.log(res)
      }
    })
  },

  /**
   * 全选图标点击事件
   */
  allChoiceTap: function() {
    var that = this
    that.setData({
      allChoice: !that.data.allChoice
    })
    var goodsList = that.data.goodsList
    goodsList.forEach(function (el) {
      el.isChoice = that.data.allChoice
    })
    that.setData({
      goodsList,
    })
    this.countMoney()
  },
  /**
   * 商品勾选点击事件
   */
  isChoiceTap: function(e) {
    var curGoodsIndex = e.currentTarget.dataset.index
    var goodsList = this.data.goodsList
    goodsList[curGoodsIndex].isChoice = !goodsList[curGoodsIndex].isChoice
    console.log(goodsList)
    this.setData({
      goodsList,
    })
    this.countMoney()
  },
  /**
   * 购买数量减一点击事件
   */
  minusTap: function(e) {
    var curGoodsIndex = e.currentTarget.dataset.index
    var goodsList = this.data.goodsList
    if (goodsList[curGoodsIndex].buyNum < 2) {
      return
    }
    goodsList[curGoodsIndex].buyNum = goodsList[curGoodsIndex].buyNum - 1
    this.setData({
      goodsList,
    })
    this.countMoney()
  },
  /**
   * 购买数量加一点击事件
   */
  addTap: function(e) {
    var curGoodsIndex = e.currentTarget.dataset.index
    var goodsList = this.data.goodsList
    goodsList[curGoodsIndex].buyNum = goodsList[curGoodsIndex].buyNum + 1
    this.setData({
      goodsList,
    })
    this.countMoney()
  },
  /**
   * 计算总价
   */
  countMoney: function() {
    var goodsList = this.data.goodsList
    var allMoney = 0
    var goodsCount = 0
    goodsList.forEach(function (el) {
      if (el.isChoice) {
        goodsCount++
        allMoney += el.buyNum * el.price
      }
    })
    this.setData({
      allMoney,
      goodsCount
    })
  },
  /**
   * 将商品从购物车移除
   */
  handleTap: function(e) {
    var that = this
    var delGoodsindex = e.currentTarget.dataset.index
    wx.showModal({
      title: strings.title,
      content: strings.isDelGoods,
      success: function(res) {
        if(res.confirm) {
          var goodsList = that.data.goodsList
          goodsList.splice(delGoodsindex,1)
          that.setData({
            goodsList
          })
          that.countMoney()
        }
      }
    })
  },
  /**
   * 点击结算，将计算的金额传入
   */
  goOrderTap: function() {
    var allMoney = this.data.allMoney
    wx.navigateTo({
      url: '../fillorder/index?allMoney=' + allMoney,
    })
  }
})